09. Collections - Map and Set

035ND C01 L01 A11 MAP AND SET

In this section I am going to show you the Map and Set interfaces.

Map stores an element in key and element fashion, that is, when you store an element, you will give it a key for Map to retrieve and manipulate it easily. Set stores elements by using hashing.

Two common Map implementations are HashMap and TreeMap.

Two common Set implementations are HashSet and TreeSet.

Resources:

Map: https://docs.oracle.com/javase/8/docs/api/java/util/Map.html

Set: https://docs.oracle.com/javase/8/docs/api/java/util/Set.html

Map and Set

Which set maintains the order of elements?

SOLUTION: TreeSet

Map and Set Q2

Map<String, String> map = new HashMap<>(); 
map.put("Name", "Aman"); 
map.put("Address", "Kolkata"); 
map.compute("Name", (key, val)  -> val.concat(" Singh")); 
System.out.println(map.get(“Name”));

What is the console output?

SOLUTION: Aman Singh

Map And Set Exercise

Task Description:

Please complete the “words frequency” coding problem.

Give a list of strings, calculate and print the frequency of each string based on alphabetical order.

Example1: input {“abc”, “bcd”, “abc”}, output “abc”|2, “bcd”|1

Task List:

035ND C01 L01 A12 MAP AND SET WALKTHROUGH